package com.jqk.mybatis.plus.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jqk.mybatis.plus.entity.User;
import com.jqk.mybatis.plus.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:jiangqikun
 * @Date:2022/8/22 19:56
 **/

@RestController
@Log4j2
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("insert-users")
    public String insertUsers() {
        ArrayList<User> users = Lists.newArrayList();
        for (int i = 0; i < 20; i++) {
            User user = getUser(i);
            users.add(user);
        }
        userService.insertUsers(users);
        return "success";
    }

    @DeleteMapping("delete-by-id")
    public String deleteById(@NotNull String userId) {
        userService.deleteById(userId);
        return "success";
    }

    @GetMapping("get-users-page")
    public List<User> getUusersPage(){
        Page<User> usersPage = userService.getUusersPage(1, 10);
        return usersPage.getRecords();
    }

    private User getUser(Integer random) {
        return User.builder()
                .name("名称" + random)
                .age(random)
                .address("地址" + random)
                .build();
    }


}
